from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import urllib2
from urllib2 import HTTPError, URLError
import BeautifulSoup

class ContentSave(webapp.RequestHandler):
    def get(self):
        out = self.response.out
        try:
                    request = urllib2.Request("http://timesofindia.indiatimes.com/rssfeedstopstories.cms")
                    opener = urllib2.build_opener()   
                    request.add_header('User-Agent','TOIHEAD/1.0 +http://toihead.appspot.com/') 
                    rssfeed = opener.open(request).read()
                    bs = BeautifulSoup.BeautifulSoup()
                    bs.feed(rssfeed)
                    links = bs.findAll('link')
                    for link in links:
                        linkstr=link.renderContents()
                        if(linkstr.startswith('http://timesofindia') and linkstr!='http://timesofindia.indiatimes.com'):
                            out.write("store?")
        
        
        except HTTPError,e: 
                    print "Error:", e.code
        except URLError, e:
                    print 'We failed to reach a server.'
                    print 'Reason: ', e.reason